package com.kcnet.qk.controller.group;

import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kcnet.qk.constant.Const;
import com.kcnet.qk.constant.StringConst;
import com.kcnet.qk.controller.BasePeopleController;
import com.kcnet.qk.util.FBStringUtil;
import com.kcnet.qk.util.NodeUtil;
import com.kcnet.qk.util.PreferenceHelper;
import com.kcnet.qk.util.RootShell;

import org.greenrobot.eventbus.EventBus;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jesse.nativelogger.NLogger;

import static com.kcnet.qk.constant.Const.LIST_VIEW;

/**
 * @Auther: 赵浩
 * @Date: 2018-04-27 15:34
 * @Description: 邀请好友进组
 */
public class TeamRqFriendCTR extends BasePeopleController {
    private static TeamRqFriendCTR releaseimgctr;
    private static String TAG = "邀请好友进组";


    private boolean isJoinGroupManger;
    private boolean isJoinGroupDetail;
    private boolean isJoinGroupDetailList;
    private boolean isJoinNumberDetail;
    private boolean isJoinYaoQing;
    private String content;
    private int errorCount;



    public static TeamRqFriendCTR getInstance() {
        if (releaseimgctr == null) {
            synchronized (TeamRqFriendCTR.class) {
                if (releaseimgctr == null) {
                    releaseimgctr = new TeamRqFriendCTR();
                }
            }
        }
        return releaseimgctr;
    }

    @Override
    public void doTask(AccessibilityEvent event) {
        mClassName = (String) event.getClassName();
        mType = event.getEventType();

        if (!initFbMain && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.FbUI.FBLauncherUI)) {
            NLogger.d(TAG, "1、点击更多到对应模块");
            // sleepTimedDefault();
            tagTextClick(FBStringUtil.getString(StringConst.TAB_MORE));
            sleepTimedDefault();
            //滑动屏幕 然后找到小组
            Log.d(TAG, "1滚动屏幕");
            RootShell.swipe(0,800,0,600);
           // execShellCmd("input swipe 150 800 150 300");
        } else if (!initFbMain && ((mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED) && mClassName.equals(Const.LIST_VIEW))) {
            //滑动屏幕 然后找到小组
            if (tagTextClick(FBStringUtil.getString(StringConst.TEAM_XIAOZU))) {
                initFbMain = true;
                isJoinGroupManger = true;
                errorCount = 0;
            } else {
                if (errorCount > 10) {
                    stopTask();
                }
                errorCount++;
                NLogger.d(TAG, "继续滚动屏幕");
                sleepTime(500);
                RootShell.swipe(0,800,0,700);
                  sleepTime(500);
            }
        } else if (isJoinGroupManger && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.FbUI.FBIMMERUI)) {
            NLogger.d(TAG, "2、进入小组管理界面 ");
        } else if (isJoinGroupManger && ((mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED) && mClassName.equals("android.support.v7.widget.RecyclerView"))) {
            NLogger.d(TAG, "3、查找对应的小组是否存在");
         //   beginFindTeam();
           // RootShell.swipe(0,800,0,700);
           //sleepTime(500);
            AccessibilityNodeInfo nodeInfo = findTextNodeInfo(content);
            if (nodeInfo != null) {
                NLogger.d(TAG, "3、找到存在对应的小组");
                if (tagTextClickShell(nodeInfo)) {
                    isJoinGroupManger = false;
                    isJoinGroupDetail = true;
                    errorCount = 0;
                    NLogger.d(TAG, "4、点击进入小组");
                }
            } else {
                if (errorCount > 10) {
                    stopTask();
                }
                errorCount++;
                RootShell.swipe(0,800,0,600);
                sleepTime(1500);
            }
        } else if (isJoinGroupDetail && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.FbUI.FBIMMERUI)) {
            NLogger.d(TAG, "5 .进入小组详情");
            isJoinGroupDetailList = true;
            isJoinGroupDetail = false;

        } else if (isJoinGroupDetailList && ((mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED) && mClassName.equals("android.support.v7.widget.RecyclerView"))) {
            NLogger.d(TAG, "6 .进入小组详情 点击按钮");
            sleepTimedDefault();
            if ( RootShell.click(250,394)) {
                isJoinNumberDetail = true;
                isJoinGroupDetailList = false;
            }
        } else if (isJoinNumberDetail && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.FbUI.FBIMMERUI)) {
            NLogger.d(TAG, "7 .进入成员管理列表");
            sleepTimedDefault();
            if (RootShell.click(470,40)) {
                isJoinNumberDetail = false;
                isJoinYaoQing = true;
            }
        } else if (isJoinYaoQing && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.FbUI.FBIMMERUI)) {
            NLogger.d(TAG, "8 .进入邀请好友界面");
        } else if (isJoinYaoQing && ((mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED) && mClassName.equals(Const.LIST_VIEW))) {
            NLogger.d(TAG, "9 开始邀请好友");
            beginAddFriend();
        }
    }


    /**
     * 執行添加推薦的好友
     */
    private void beginFindTeam() {
        AccessibilityNodeInfo scrllorNodeInfo = null;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {
                List<AccessibilityNodeInfo> scrllorNodeInfos = NodeUtil.traverseNodeByClassList(root, "android.support.v7.widget.RecyclerView");
                if (scrllorNodeInfos.size() > 0) {
                    scrllorNodeInfo = scrllorNodeInfos.get(0);
                }
                if (scrllorNodeInfo == null) {
                    return;
                }
                for (int i = 0; i < scrllorNodeInfo.getChildCount(); i++) {

                    AccessibilityNodeInfo as = scrllorNodeInfo.getChild(i);
                    for(int q=0;q<as.getChildCount();q++){
                        AccessibilityNodeInfo text = as.getChild(q);
                        if(text.getText()!=null){
                            if(text.getText().toString().contains(content)){

                                NLogger.d(TAG, "4 找到了对应小组 点击下 "+tagTextClickShell(text)+" name:"+text.getText().toString());
                                isJoinGroupManger = false;
                                isJoinGroupDetail = true;
                                break;
                            }
                        }
                    }


                    if (i == scrllorNodeInfo.getChildCount() - 1) {
                        if (scrllorNodeInfo != null) {
                            NodeUtil.performScroller(scrllorNodeInfo);
                            break;
                        }
                    }
                }
            }
        }
    }


    /**
     * 執行添加推薦的好友
     */
    private void beginAddFriend() {
        AccessibilityNodeInfo scrllorNodeInfo = null;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {
                List<AccessibilityNodeInfo> scrllorNodeInfos = NodeUtil.traverseNodeByClassList(root, LIST_VIEW);
                if (scrllorNodeInfos.size() > 0) {
                    scrllorNodeInfo = scrllorNodeInfos.get(0);
                }
                if (scrllorNodeInfo == null) {
                    return;
                }
                for (int i = 0; i < scrllorNodeInfo.getChildCount(); i++) {
                    if (currentAddCount == maxAddCount) {
                        currentAddCount = 0;
                        tagTextClick(FBStringUtil.getString(StringConst.TIMELINE_WANCHENG));
                        stopTask();
                        break;
                    }

                    AccessibilityNodeInfo as = scrllorNodeInfo.getChild(i);
                    boolean flag =  NodeUtil.performClick(as);
                    if(flag){
                        currentAddCount++;
                    }
                    NLogger.d(TAG, "点击了好友" + flag);

                    if (i == scrllorNodeInfo.getChildCount() - 1) {
                        if (scrllorNodeInfo != null) {
                            NodeUtil.performScroller(scrllorNodeInfo);
                            break;
                        }
                    }
                }
            }
        }
    }


    public void setParms(int maxAddCount, int minAddTime, int maxAddTime, String content) {
        //  stopTask();
        EventBus.getDefault().post(new Boolean(true));
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        this.maxAddCount = maxAddCount;
        this.minAddTime = minAddTime;
        this.maxAddTime = maxAddTime;
        this.currentAddCount = 0;
        this.content = content;
        this.errorCount = 0;
        this.initFbMain = false;

        isJoinGroupManger = false;
        isJoinGroupDetail = false;
         isJoinYaoQing = false;
    }
}
